$(function(){
	inpege.bind();
	inpege.setDataList();
	nameToIdUtil.getSelectValue("approvalUser","user/getDepartmentUser",{"deptName":"仓库"},1);
});
var inMaterialTable;
var inpege = { 
	bind:function(){
		$("#inmaterialName").bind("keyup",function(){
			inpege.setDataList();
		});
		$("#inmodal").bind("keyup",function(){
			inpege.setDataList();
		});
		$("#inmaterialType").bind("keyup",function(){
			inpege.setDataList();
		});
		$("#inproviderName").bind("keyup",function(){
			inpege.setDataList();
		});
		$("#incolor").bind("keyup",function(){
			inpege.setDataList();
		});
		$("#incolorDescription").bind("keyup",function(){
			inpege.setDataList();
		});
		$("#queryMaterial").bind("click",function(){
			var object =new Object();
			object.materialName=$("#inmaterialName").val();
			object.model=$("#inmodal").val();
			object.materialType=$("#inmaterialType").val();
			object.providerName=$("#inproviderName").val();
			object.color=$("#incolor").val();
			object.colorDescription=$("#incolorDescription").val();
			inpege.getInMaterial(object);
		});
		//输入入库数
		 $('#inMaterialTable tbody').on('keyup', '.inCount', function () {
			 var count=$(this).val();
			 if(isNaN(Number(count)) || count===""){
				 layAlert("入库数必须是数字");
				 $(this).val(0);
				 return; 
			 }
			 if(Number(count)<0){
				 layAlert("入库数必须大于0");
				 $(this).val(0);
				 return;
			 }  
			 if (!/^(0|[1-9]\d*)(\.\d{0,2})?$/.test(Number(count))) {
					layAlert("入库数量小数点后只能是0-2位");
					$(this).val(0);
					return false; 
			 }
		 });
		 //输入入库数
		 $('#modalStock').bind('keyup',function () {
			 var re=/^[1-9]+[0-9]*]*$/;
			 var count=$(this).val();
			 if(!re.test(count)){
				 count=count.substring(0,count.length-1);
				 $(this).val(count);
			 }
		 });
		 //保存入库记录
		 $("#savdInWarehouseBtn").bind("click",function(){
			 var approvalUser=$("select[name=approvalUser]").val();
			 if(approvalUser==""){
				 layAlert("请选择验收人");
				 return false;
			 }
			 var array=new Array();
			 var checks = $("input[name='inCheckBox']:checked");
			 for(var i=0;i<checks.length;i++){
				 var $input = checks.eq(i);
				 var object=new Object();
					var count = $input.parent().prevAll().eq(3).children("input").val();
					var inCountPrice = $input.parent().prevAll().eq(2).children("input").val();
					if(inCountPrice.trim()!==""){
						if(isNaN(Number(inCountPrice))){
							layAlert("入库单价必须是数字");
							$input.parent().prevAll().eq(2).children("input").val("");
							return;
						}
						if(Number(inCountPrice)<=0){
							 layAlert("入库单价必须大于0");
							 $input.parent().prevAll().eq(2).children("input").val("");
							 return;
						}  
						if (!/^(0|[1-9]\d*)(\.\d{0,2})?$/.test(Number(inCountPrice))) {
							layAlert("入库单价小数点后只能是0-2位");
							$input.parent().prevAll().eq(2).children("input").val("");
							return; 
						}
					}
					 if(isNaN(Number(count)) || count===""){
						 layAlert("入库数必须是数字");
						 $input.parent().prevAll().eq(3).children("input").val(0);
						 return;
					 } 
					 if(Number(count)<=0){
						 layAlert("入库数必须大于0");
						 $input.parent().prevAll().eq(3).children("input").val(0);
						 return; 
					 }  
					 if (!/^(0|[1-9]\d*)(\.\d{0,2})?$/.test(Number(count))) {
							layAlert("入库数量小数点后只能是0-2位");
							$input.parent().prevAll().eq(3).children("input").val(0);
							return;
					 }
					object.approvalUser=approvalUser;//验收人
					object.type=4;//无采购单 不增加物料入库
					object.materialCode=$input.attr("materialCode");//物料代码
					//物料名称
					object.materialName=$input.parents("tr").children("td").eq(0).text();
					//供应商
					object.providerName=$input.parents("tr").children("td").eq(2).text();
					//规格
					object.model=$input.parents("tr").children("td").eq(3).text();
					//编号
					object.color=$input.parents("tr").children("td").eq(4).text();
					object.count=Number(count);//入库数 
					object.inCountPrice = inCountPrice;//入库金额
					object.positions=$input.parent().prevAll().eq(1).children("input").val();//仓位
					object.remark=$input.parent().prevAll().eq(0).children("input").val();//备注
					array.push(object);
			 }
			 if(array.length<=0){
				 layAlert("请勾选入库行");
				 return false;
			 }
			 for(var i=0;i<array.length;i++){
				 if(array[i].count==''){
					 layAlert("入库数不能为空");
					 return false;
				 }
			 }
			 ajaxUtil.ajaxRuquest("companyMaterial/savdInWarehouseNotPurchase",{"array":JSON.stringify(array)},function(data){
				 layer.msg(data,{icon:1});
				 inMaterialTable.ajax.reload( null, false);
//				 page.getPurchaseMaterialDate(purchaseId,type);
			 }); 
		 });
	},
	//页面公用方法
	setDataList:function(){
		var object =new Object();
		object.materialName=$("#inmaterialName").val();
		object.model=$("#inmodal").val();
		object.materialType=$("#inmaterialType").val();
		object.providerName=$("#inproviderName").val();
		object.color=$("#incolor").val();
		object.colorDescription=$("#incolorDescription").val();
		$.ajax({
			url:'companyMaterial/getVagueCompanyMaterialByMap',
			data:object,
			success:function(data){
				if(data.result){
					data=data.data;
					var materialNameArr = [];
					var materialTypeArr = [];
					var modelArr = [];
					var colorArr = [];
					var providerNameArr = [];
					var colorArr = [];
					var colorDescriptionArr = [];
				    for(var i=0;i<data.length;i++){ 
				    	materialNameArr.push(data[i]['materialName']);
				    	materialTypeArr.push(data[i]['materialType']);
				    	modelArr.push(data[i]['model']);
				    	providerNameArr.push(data[i]['providerName']);
				    	colorArr.push(data[i]['color']===null?'':data[i]['color']);
				    	colorDescriptionArr.push(data[i]['colorDescription']===null?'':data[i]['colorDescription']);
				    }
				    //过滤
				    var materialNameList = inpege.getFliterArr(materialNameArr);
				    var materialTypeList = inpege.getFliterArr(materialTypeArr);
				    var modelList = inpege.getFliterArr(modelArr);
				    var providerNameList = inpege.getFliterArr(providerNameArr);
				    var colorList = inpege.getFliterArr(colorArr);
				    var colorDescriptionList = inpege.getFliterArr(colorDescriptionArr);
				    //拼接
					$("#inmaterialNamelList").html(materialNameList);
					$("#inmodalList").html(modelList);
					$("#inmaterialTypeList").html(materialTypeList);
					$("#inproviderNameList").html(providerNameList);
					$("#incolorList").html(colorList);
					$("#incolorDescriptionList").html(colorDescriptionList);
				}
			}
		});
	},
	//获取过滤数据
	getFliterArr:function(dataArr){
		var fliterData =  dataArr.filter(function (item, index, self) {
	    	  return self.indexOf(item) == index;
	    }); 
		return inpege.appendFliterArrData(fliterData);
	},
	//拼接过滤后的数据
	appendFliterArrData:function(fliterData){
		var html="";
		for(var i=0;i<fliterData.length;i++){
			html+="<option value="+fliterData[i]+">"+fliterData[i]+"</option>"
		}
		return html;
	},
	getInMaterial:function(object){
		inMaterialTable=ajaxUtil.dataTablesAjaxNotFxiaction("inMaterialTable",10,"companyMaterial/getVagueCompanyMaterialByMapToPage",
				object,[
				{"data":"materialName"},
				{"data":"materialType"},
				{"data":"providerName"},
				{"data":"model"},
				{"data":"color"},
				{"data":"colorDescription"},
				{"data":"unitPrice"},
				{"data":"count"},
				{"data":"materialName",render:function(data,type,row){
					return "<input type='text' class='input-text inCount'  />";
				}},  
				{"data":"materialName",render:function(data,type,row){
					return "<input type='text' class='input-text inCountPrice'  />";
				}},  
				{"data":"materialName",render:function(data,type,row){
					return "<input type='text' class='input-text' >";
				}},  
				{"data":"materialName",render:function(data,type,row){
					return "<input type='text' class='input-text' >";
				}}, 
				{"data":"id",render:function(data,type,row){
					return "<input type='checkbox' name='inCheckBox' value='"+data+"' materialCode='"+row.materialCode+"' />";
				}}  
				
		]);
	}
}